Newlines in Macro Arguments

Contents|Index|Previous|Next
Newlines in macro arguments

Traditional macro processing carries forward all newlines in macro arguments into the expansion of the macro. This means that, if some of the arguments are substituted more than once, or not at all, or out of order, newlines can be duplicated, lost, or moved around within the expansion. If the expansion consists of multiple statements, then the effect is to distort the line numbers of some of these statements. The result can be incorrect line numbers, in error messages or displayed in a debugger.

The GNU C preprocessor operating in ANSI C mode adjusts appropriately for multiple use of an argument—the first use expands all the newlines, and subsequent uses of the same argument produce no newlines. But even in this mode, it can produce incorrect line numbering if arguments are used out of order, or not used at all. What follows is an example illustrating this problem.

    #define ignore_second_arg(a,b,c) a; c

    ignore_second_arg (foo (),
                      ignored (),
                      syntax error);

The syntax error triggered by the tokens “syntax error” results in an error message citing the line containing “ignored (),” even though the statement of “syntax error);” is the line containing the error.

Top|Contents|Index|Previous|Next